layui.config({
    base : "/dhjdglxt/modules/util/"//自定义加载的模块
}).use(['form','layer','jquery','layedit','laydate','laypage','table','commUtil','element'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;
    var table = layui.table;
    var commUtil = layui.commUtil;
    var element = layui.element;
    var basePath="/dhjdglxt";

    laydate.render({
        elem: '#djsj',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#startTime',
        trigger: 'click',
        type:'date'
    });

    laydate.render({
        elem: '#endTime',
        trigger: 'click',
        type:'date'
        ,done:function (value,date) {
            var jhdxxyckssjT=$("#jhdxxyckssjT").val();
            if(jhdxxyckssjT==null||jhdxxyckssjT==undefined||jhdxxyckssjT==""){
                layer.msg("请选择机身计划大修下一次开始时间T以计算相应数据", {icon: 0});
            }else {
                var  betweenDay=getDaysBetween(value,jhdxxyckssjT);
                if(betweenDay>180){
                    $("#tzrqqbnnjhdx").val("不预警");
                }else{
                    $("#tzrqqbnnjhdx").val("预警");
                }
            }
            var dxksslQ1=$("#dxksslQ1").val();
            if(dxksslQ1!=null&&dxksslQ1!=undefined&&dxksslQ1!=""){
                var  betweenDay=getDaysBetween(value,dxksslQ1);
                var  oldVal=$("#tzrqqbnnyqdx").val();
                if(betweenDay>180&&"预警"!=oldVal){
                    $("#tzrqqbnnyqdx").val("不预警");
                }else{
                    $("#tzrqqbnnyqdx").val("预警");
                }
            }
            var dxkssjQ2=$("#dxkssjQ2").val();
            if(dxkssjQ2!=null&&dxkssjQ2!=undefined&&dxkssjQ2!=""){
                var  betweenDay=getDaysBetween(value,dxkssjQ2);
                var  oldVal=$("#tzrqqbnnyqdx").val();
                if(betweenDay>180&&"预警"!=oldVal){
                    $("#tzrqqbnnyqdx").val("不预警");
                }else{
                    $("#tzrqqbnnyqdx").val("预警");
                }
            }
        }
    });
    laydate.render({
        elem: '#proctionDate',
        trigger: 'click',
        type:'date'
        ,done:function (value,date) {

        }
    });
    laydate.render({
        elem: '#useDate',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#jrygrq',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#tcygrq',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#offDate',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#gjDate',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#qfDate',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#dtyxDate',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#dtbfDate',
        trigger: 'click',
        type:'date'
        ,done:function (value,date) {
            countDtzzyxqx(value)
        }
    });

    laydate.render({
        elem: '#dxkssjQ',
        trigger: 'click',
        type:'date'
        ,done:function (value,date) {
            //getDataByYearAndQuarter(value);
            var jhxrwmc=$("#jhxrwmc").val();
            if(jhxrwmc==null||jhxrwmc==undefined||jhxrwmc==""){
                layer.msg("请继续选择计划修任务名称以计算相应数据", {icon: 0});
            }else {
                countQ1AndQ2(value,jhxrwmc);
            }
            /*alert(value);
            alert(jhxrwmc);
            $("#dxksslQ1").val(value);*/
        }
    });
    laydate.render({
        elem: '#dxksslQ1',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#dxkssjQ2',
        trigger: 'click',
        type:'date'
    });
    laydate.render({
        elem: '#jhdxxyckssjT',
        trigger: 'click',
        type:'date'
        ,done:function (value,date) {
        var endTime=$("#endTime").val();
        if(endTime==null||endTime==undefined||endTime==""){
            layer.msg("请选择退租日以计算相应数据", {icon: 0});
        }else {
            var  betweenDay=getDaysBetween(endTime,value);
            if(betweenDay>180){
                $("#tzrqqbnnjhdx").val("不预警");

            }else{
                $("#tzrqqbnnjhdx").val("预警");
            }
        }
    }
    });

    /*--------------*/
    //计算剩余飞行小时
    $("#yyfxxs").change(function () {
        var yyfxxs=$("#yyfxxs").val();
        if(!isNaN(parseFloat(yyfxxs))){
            var syfxxs=60000-parseFloat(yyfxxs);
            $("#syfxxs").val(syfxxs);
            //计算DSG到期日
            var lyl=$("#lyl").val();
            if(!isNaN(parseFloat(lyl))){
                countDsg(syfxxs,lyl)
            }

        }else {
            layer.msg("请确保输入的已有飞行小时是数字类型", {icon: 0});
            $("#yyfxxs").val("");
        }
    });
    $("#lyl").change(function () {
        var lyl=$("#lyl").val();
        if(!isNaN(parseFloat(lyl))){
            //计算DSG到期日
            var yyfxxs=$("#yyfxxs").val();
            if(!isNaN(parseFloat(yyfxxs))){
                var syfxxs=60000-parseFloat(yyfxxs);
                countDsg(syfxxs,lyl)
            }

        }else {
            layer.msg("请确保输入的利用率是数字类型", {icon: 0});
            $("#lyl").val("");
        }
    });

    //计划修任务名称
    form.on("select(jhxrwmc)",function (data) {
        console.log("--data--"+JSON.stringify(data))
        var dxkssjQ=$("#dxkssjQ").val();
        if(dxkssjQ==null||dxkssjQ==undefined||dxkssjQ==""){
            layer.msg("请继续选择机身最近大修开始时间Q以计算相应数据", {icon: 0});
        }else {
            countQ1AndQ2(dxkssjQ,data.value);
        }


    });

    //计算Q1和Q2的值
    function countQ1AndQ2(q,c){
        /*var year = new Date(q).getFullYear();
        var month = new Date(q).getMonth() + 1;
        var day = new Date(q).getDate();
        alert("---"+year);
        alert("---"+month);
        alert("---"+day);*/
        var x=parseFloat(c)*12;
        var date = new Date(q);
        //机身预期下一次大修开始时间Q1
        var dateDemoQ1 = date.setMonth(date.getMonth()+x);
        var q1 = new Date(dateDemoQ1);
        var yearQ1 = q1.getFullYear();
        var monthQ1 = q1.getMonth() + 1;
        var dayQ1 = q1.getDate();
        if(monthQ1<10)monthQ1="0"+monthQ1;
        if(dayQ1<10)dayQ1="0"+dayQ1;
        var dateStrQ1=yearQ1+"-"+monthQ1+"-"+dayQ1;
        //机身预期再下一次大修开始时间Q2（q1基础上再加x）
        var dateDemoQ2 = date.setMonth(date.getMonth()+x);
        var q2 = new Date(dateDemoQ2);
        var yearQ2 = q2.getFullYear();
        var monthQ2 = q2.getMonth() + 1;
        var dayQ2 = q2.getDate();
        if(monthQ2<10)monthQ2="0"+monthQ2;
        if(dayQ2<10)dayQ2="0"+dayQ2;
        var dateStrQ2=yearQ2+"-"+monthQ2+"-"+dayQ2;

        $("#dxksslQ1").val(dateStrQ1);
        $("#dxkssjQ2").val(dateStrQ2);

    };

    //计算DSG的值
    function countDsg(syfxxs,lyl){
        var date = new Date();
        var dsgDqr=syfxxs/parseFloat(lyl);
        //机身预期下一次大修开始时间Q1
        var newDate = date.setDate(date.getDate()+dsgDqr);
        var q1 = new Date(newDate);
        var yearQ1 = q1.getFullYear();
        var monthQ1 = q1.getMonth() + 1;
        var dayQ1 = q1.getDate();
        if(monthQ1<10)monthQ1="0"+monthQ1;
        if(dayQ1<10)dayQ1="0"+dayQ1;
        var dateStrQ1=yearQ1+"-"+monthQ1+"-"+dayQ1;
        $("#dsgDqr").val(dateStrQ1);
    }

    //计算天数
    function  getDaysBetween(dateString1,dateString2){
        var  startDate = Date.parse(dateString1);
        var  endDate = Date.parse(dateString2);
        if (startDate>endDate){
            return 0;
        }
        if (startDate==endDate){
            return 1;
        }
        var days=(endDate - startDate)/(1*24*60*60*1000);
        return  days;
    }

    //计算电台执照有效期限和电台执照监控报警日期
    function countDtzzyxqx(date){
        var date = new Date(date);
        //电台执照有效期限+3年
        var dtzzyxqx = date.setFullYear(date.getFullYear()+3);
        var q1 = new Date(dtzzyxqx);
        var yearQ1 = q1.getFullYear();
        var monthQ1 = q1.getMonth() + 1;
        var dayQ1 = q1.getDate();
        if(monthQ1<10)monthQ1="0"+monthQ1;
        if(dayQ1<10)dayQ1="0"+dayQ1;
        var dateStrQ1=yearQ1+"-"+monthQ1+"-"+dayQ1;
        //电台执照监控报警日期 (+3年-3月)
        var dtzzjkbjrq = date.setMonth(date.getMonth()-3);
        var q2 = new Date(dtzzjkbjrq);
        var yearQ2 = q2.getFullYear();
        var monthQ2 = q2.getMonth() + 1;
        var dayQ2 = q2.getDate();
        if(monthQ2<10)monthQ2="0"+monthQ2;
        if(dayQ2<10)dayQ2="0"+dayQ2;
        var dateStrQ2=yearQ2+"-"+monthQ2+"-"+dayQ2;

        $("#dtyxDate").val(dateStrQ1);
        $("#dtzzjkbjrq").val(dateStrQ2);

    };

    //计划选择
    $.get(basePath + '/service/airplanebaseinfo/airplanebaseinfo/ztree', function (data) {
        if(data){
            var optionStr="";
            for(var i=0;i<data.length;i++){
                var select="";
                if($('#gljhid').val()==data[i].id){
                    select=" selected='selected' "
                }
                optionStr+="<option "+select+" value='"+data[i].id+"'>"+data[i].name+"</option>";
            }
            $("#gljh").append(optionStr);
            form.render('select');
        }
    });
    //监听提交
    form.on('submit(formSubmit)', function(data){
        var users = $("#nextUser").val();
        var parms = "";
        var title = ""
        parms = "type=users&value=nextUser&name=nextUserName&chkType=radio";
        title = "流程人员选择选择";
        openGneDiv("380px", "400px", basePath + "/service/ztree/ztreeInfo?" + parms, title);
        return false;
    });

    $("#nextUserName").click(function(){
        var users = $("#nextUser").val();
        var parms = "";
        var title = ""
        parms = "type=users&value=nextUser&name=nextUserName&chkType=radio";
        title = "流程人员选择选择";
        openGneDiv("380px", "400px", basePath + "/service/ztree/ztreeInfo?" + parms, title);
        return false;

    });
    $("#closeWin").click(function(){

        layer.closeAll();
    });

    //监听保存
    form.on('submit(saveSubmit)', function(data){
        var recId='0';
        data.field.status = 0;
        if(data.field.recId!=''){
            recId=data.field.recId;
        }
        $.ajax({
            type: "PUT",
            url: basePath+'/service/airplanebaseinfo/airplanebaseinfo/'+recId,
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    parent.location.reload();
                } else {
                    layer.msg("操作失败", {icon: 0});
                }
            }
        });
    });
    //监听保存
    form.on('submit(saveUphold)', function(data){
        var recId='0';
        if(data.field.recId!=''){
            recId=data.field.recId;
        }
        $.ajax({
            type: "PUT",
            url: basePath+'/service/airplanebaseinfo/airplanebaseinfo/uphold/'+recId,
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    parent.location.reload();
                } else {
                    layer.msg("操作失败", {icon: 0});
                }
            }
        });
    });
    //监听保存
    form.on('submit(save)', function(data){
        var recId='0';
        if(data.field.recId!=''){
            recId=data.field.recId;
        }
        $.ajax({
            type: "PUT",
            url: basePath+'/service/airplanebaseinfo/airplanebaseinfo/'+recId,
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    parent.location.reload();
                } else {
                    layer.msg("操作失败", {icon: 0});
                }
            }
        });
    });
    //三证有效性维护办理保存
    form.on('submit(saveszbg)', function(data){
        parent.$("#transContents").val(data.field.transContents);
        var recId='0';
        if(data.field.recId!=''){
            recId=data.field.recId;
        }
        $.ajax({
            type: "PUT",
            url: basePath+'/service/airplanebaseinfo/airplanebaseinfo/oacwsave',
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    try{
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        if(index==null||typeof(index)=='undefined'){
                            var iframeId=parent.document.getElementsByTagName("iframe")[1].id;
                            index=iframeId.substring(iframeId.length-1);
                        }
                        parent.layer.close(index); //再执行关闭
                        parent.document.getElementsByTagName("iframe")[0].contentWindow.location.reload(true);
                    }catch(e){
                    }
                } else {
                    layer.msg("操作失败", {icon: 0});
                }
            }
        });


    });


    //监听保存
    form.on('submit(saveghbg)', function(data){

        var users = $("#nextUser").val();
        var parms = "";
        var title = ""
        parms = "type=users&value=nextUser&name=nextUserName&chkType=radio";
        title = "流程人员选择选择";
        openGneDiv("380px", "400px", basePath + "/service/ztree/ztreeInfo?" + parms, title);
        return false;

    });
    //监听保存
    form.on('submit(savecwbg)', function(data){

        var users = $("#nextUser").val();
        var parms = "";
        var title = ""
        parms = "type=users&value=nextUser&name=nextUserName&chkType=radio";
        title = "流程人员选择选择";
        openGneDiv("380px", "400px", basePath + "/service/ztree/ztreeInfo?" + parms, title);
        return false;

    });
    //监听保存
    form.on('submit(flowSubmit)', function(data){
        parent.$("#transContents").val(data.field.transContents);
        var recId='0';
        if(data.field.recId!=''){
            recId=data.field.recId;
        }
        $.ajax({
            type: "PUT",
            url: basePath+'/service/airplanebaseinfo/airplanebaseinfo/saveHt/'+recId,
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    try{
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        if(index==null||typeof(index)=='undefined'){
                            var iframeId=parent.document.getElementsByTagName("iframe")[1].id;
                            index=iframeId.substring(iframeId.length-1);
                        }
                        parent.layer.close(index); //再执行关闭
                        parent.document.getElementsByTagName("iframe")[0].contentWindow.location.reload(true);
                    }catch(e){
                    }
                } else {
                    layer.msg("操作失败", {icon: 0});
                }
            }
        });

    });
    //监听保存
    form.on('submit(flowpwSubmit)', function(data){
        parent.$("#transContents").val(data.field.transContents);
        var recId='0';
        if(data.field.recId!=''){
            recId=data.field.recId;
        }
        $.ajax({
            type: "PUT",
            url: basePath+'/service/airplanebaseinfo/airplanebaseinfo/savePw/'+recId,
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    try{
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        if(index==null||typeof(index)=='undefined'){
                            var iframeId=parent.document.getElementsByTagName("iframe")[1].id;
                            index=iframeId.substring(iframeId.length-1);
                        }
                        parent.layer.close(index); //再执行关闭
                        parent.document.getElementsByTagName("iframe")[0].contentWindow.location.reload(true);
                    }catch(e){
                    }
                } else {
                    layer.msg("操作失败", {icon: 0});
                }
            }
        });

    });
    //监听保存
    form.on('submit(saveAir)', function(data){
        parent.$("#transContents").val(data.field.transContents);
        var recId='0';
        if(data.field.recId!=''){
            recId=data.field.recId;
        }
        $.ajax({
            type: "PUT",
            url: basePath+'/service/airplanebaseinfo/airplanebaseinfo/saveAir/'+recId,
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    try{
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        if(index==null||typeof(index)=='undefined'){
                            var iframeId=parent.document.getElementsByTagName("iframe")[1].id;
                            index=iframeId.substring(iframeId.length-1);
                        }
                        parent.layer.close(index); //再执行关闭
                        parent.document.getElementsByTagName("iframe")[0].contentWindow.location.reload(true);
                    }catch(e){
                    }
                } else {
                    layer.msg("操作失败", {icon: 0});
                }
            }
        });
    });
    //监听保存
    form.on('submit(saveOaInfo)', function(data){
        //var lastIframeId=parent.document.getElementsByTagName("iframe")[0].id;
        //parent.$("#"+lastIframeId).contents()[0].getElementById("transContent").value=data.field.transContents;
        parent.$("#transContents").val(data.field.transContents);
        var recId='0';
        if(data.field.recId!=''){
            recId=data.field.recId;
        }
        $.ajax({
            type: "PUT",
            url: basePath+'/service/airplanebaseinfo/airplanebaseinfo/oacwsave',
            data: data.field,
            async:false,
            dataType: "json",
            success: function(data){
                if(data.code=='0'){
                    layer.msg("操作成功", {icon: 1});
                    try{
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        if(index==null||typeof(index)=='undefined'){
                            var iframeId=parent.document.getElementsByTagName("iframe")[1].id;
                            index=iframeId.substring(iframeId.length-1);
                        }
                        parent.layer.close(index); //再执行关闭
                        parent.document.getElementsByTagName("iframe")[0].contentWindow.location.reload(true);
                    }catch(e){
                    }
                } else {
                    if(data.code=='400'){
                        layer.msg("操作失败:"+data.data.fsn, {icon: 0});
                    }else{
                        layer.msg("操作失败", {icon: 0});
                    }

                }
            }
        });
    });

    form.verify({
        nlength:function(value){
            var i,sum;
            sum=0;
            for (i= 0; i < value.length; i++) {
                if((value.charCodeAt(i)>=0)&&(value.charCodeAt(i)<=255))
                    sum=sum+1;
                else
                    sum=sum+2;
            }
            if(sum>200){
                return "只能输入200个字符或100个中文汉字!";
            }
        }
    });
    function openGneDiv(width, height, url, title) {
        $(window).one("resize", function () {
            var index2 = layui.layer.open({
                type: 2,
                title: title,
                skin: 'layui-layer-molv',
                offset: 't',
                area: [width, height],
                content: url
            })
        }).resize();
        layer.close(0);
    }
})
